package com.hui.account.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.hui.account.R;
import com.hui.account.utils.ImageUtils;

/**
 * 展示图片
 */
public class ImageDialog extends Dialog implements View.OnClickListener {

    ImageView imageView;

    public ImageDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image);

        // 1、设置屏幕顶部字体颜色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        imageView = findViewById(R.id.dialog_image);
        imageView.setOnClickListener(this);

        // 2、展示图片
        SharedPreferences preferences = getContext().getSharedPreferences("shareParam", Context.MODE_PRIVATE);
        String imageTemp = preferences.getString("imageTemp", "");
        if (!TextUtils.isEmpty(imageTemp))
            imageView.setImageBitmap(new Gson().fromJson(imageTemp, Bitmap.class));
    }

    /**
     * 设置 dialog尺寸与屏幕尺寸保持一致
     */
    public void setDialogSize() {
        Window window = getWindow();
        Display display = window.getWindowManager().getDefaultDisplay();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = display.getWidth();
        params.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(params);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_image:
                cancel();
                break;
        }
    }
}
